﻿using Microsoft.SharePoint;

namespace SPPropertyBagViewer.Helpers
{
    public static class RunAsHelper
    {
        public delegate void RunAsElevatedWeb(SPWeb spWeb);
        public delegate void RunAsElevatedList(SPList spList);
        public delegate void RunAsElevatedListItem(SPItem spItem);
        public static void RunAsElevated(this SPWeb spWeb, RunAsElevatedWeb secureCode)
        {
            if (secureCode == null)
                return;
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite elevatedSite = new SPSite(spWeb.Site.ID))
                {
                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb(spWeb.ID))
                    {
                        elevatedWeb.AllowUnsafeUpdates = true;
                        try
                        {
                            secureCode.Invoke(elevatedWeb);
                        }
                        finally
                        {
                            elevatedWeb.AllowUnsafeUpdates = false;    
                        }
                    }
                }
            });
        }

        public static void RunAsElevated(this SPList spList, RunAsElevatedList secureCode)
        {
            if (secureCode == null) return;
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite elevatedSite = new SPSite(spList.ParentWeb.Site.ID))
                {
                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb(spList.ParentWeb.ID))
                    {
                        elevatedWeb.AllowUnsafeUpdates = true;
                        try
                        {
                            SPList elevatedList = elevatedWeb.Lists[spList.ID];
                            secureCode.Invoke(elevatedList);
                        }
                        finally
                        {
                            elevatedWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }
            });
        }
        public static void RunAsElevated(this SPListItem spListItem, RunAsElevatedListItem secureCode)
        {
            if (secureCode == null) return;
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite elevatedSite = new SPSite(spListItem.ParentList.ParentWeb.Site.ID))
                {
                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb(spListItem.ParentList.ParentWeb.ID))
                    {
                        elevatedWeb.AllowUnsafeUpdates = true;
                        try
                        {
                            SPList elevatedList = elevatedWeb.Lists[spListItem.ID];
                            SPListItem elevatedListItme = elevatedList.GetItemById(spListItem.ID);
                            secureCode.Invoke(elevatedListItme);
                        }
                        finally
                        {
                            elevatedWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }
            });
        }
    }
}
